package com.bjc.sdx.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;
import com.bjc.sdx.R;
import com.bjc.sdx.adapter.MyGridAdapter;
import com.bjc.sdx.model.Fruit;
import java.util.ArrayList;

public class GridViewActivity extends Activity {
    private LayoutInflater mInflater;
    private ArrayList<Fruit> mList;
    private Button button;
    private Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gridview_layout); // 先设置布局
        mInflater = getLayoutInflater();
        mList = new ArrayList<Fruit>();
        for (int i = 0; i <= 4; i++) {
            Fruit apple = new Fruit("4K", R.drawable.apple);
            Fruit banana = new Fruit("5G", R.drawable.banana);
            Fruit grape = new Fruit("通讯录", R.drawable.grape);
            Fruit strawberry = new Fruit("休眠", R.drawable.strawberry);
            Fruit baiduCloud = new Fruit("百度云盘", R.drawable.baidu_cloud);
            mList.add(apple);
            mList.add(banana);
            mList.add(grape);
            mList.add(strawberry);
            mList.add(baiduCloud);
        }
        GridView gridView = (GridView) findViewById(R.id.gridView1);
        MyGridAdapter adapter = new MyGridAdapter(mInflater, mList);
        gridView.setAdapter(adapter);
        // 点击事件
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {
                Toast.makeText(GridViewActivity.this, "你点击的应用为：" + mList.get(position).getName(), Toast.LENGTH_SHORT).show();
            }
        });


        button = findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(GridViewActivity.this, GridViewActivity.class);
                GridViewActivity.this.startActivity(intent);
            }
        });

        button1 = findViewById(R.id.btn1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(GridViewActivity.this, ViewPagerActivity2.class);
                GridViewActivity.this.startActivity(intent);
            }
        });

    }
}
